package com.module.app.widget;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.alimuzaffar.lib.pin.PinEntryEditText;
import com.module.app.widget.keyboard.base.KeyboardHeightFragment;
import com.module.base.utils.PhoneUtils;
import com.module.app.widget.keyboard.base.KeyboardHeightFragment;
import com.module.app.R;
import com.module.frame.app.AppManager;


/**
 * @author bsnl_yanxuwen
 * @date 2020/9/29 10:27
 * Description :
 * 发送验证码弹窗2
 */
public class ShowSendView {
    String phone;
    private TextView tv_send;
    private Object object;
    private KeyboardHeightFragment fragment;
    private boolean isClickKeyboard;//点击的时候是否只隐藏键盘
    private boolean isHidePhone;

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public void setClickKeyboard(boolean clickKeyboard) {
        this.isClickKeyboard = clickKeyboard;
    }

    public void setHidePhone(boolean isHidePhone) {
        this.isHidePhone = isHidePhone;
    }

    public interface Listener {
        void onSend(String phone);

        void onSubmit(String code);

        void hide();
    }

    public Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public ShowSendView(String phone) {
        this.phone = phone;
    }


    public void hide() {
        if (fragment != null) {
            fragment.dismiss();
        }
    }

    public void show() {
        if (AppManager.getInstance().currentActivity() == null || !(AppManager.getInstance().currentActivity() instanceof FragmentActivity)) {
            return;
        }
        FragmentActivity fragmentActivity = (FragmentActivity) AppManager.getInstance().currentActivity();
        if (fragmentActivity == null || fragmentActivity.isFinishing()) {
            return;
        }
        fragment = KeyboardHeightFragment.newInstance(R.layout.app_pop_send_code, R.id.editText, "", Color.parseColor("#99000000"));
        fragment.setClickKeyboard(isClickKeyboard);
        fragment.showAllowingStateLoss(fragmentActivity.getSupportFragmentManager(), "tag");
        fragment.setListener(new KeyboardHeightFragment.Listener() {

            @Override
            public void onDismiss() {
                handler.removeCallbacksAndMessages(null);
                if (listener != null) {
                    listener.hide();
                }
            }

            @Override
            public void onComplete(View view) {
                fragment.getEditText().setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});
                view.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hide();
                    }
                });
                TextView tv_phone = view.findViewById(R.id.tv_phone);
                TextView tv_phone_tips = view.findViewById(R.id.tv_phone_tips);
                tv_phone.setText(PhoneUtils.getTelFormat(phone));
                tv_phone_tips.setVisibility(isHidePhone ? View.GONE : View.VISIBLE);
                tv_phone.setVisibility(isHidePhone ? View.GONE : View.VISIBLE);
                tv_send = view.findViewById(R.id.tv_send);
                tv_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        time = 60;
                        handler.sendEmptyMessage(0);
                        //调用发送短信接口
                        if (listener != null) {
                            listener.onSend(phone);
                        }
                    }
                });
                tv_send.performClick();
                PinEntryEditText codeEditText = view.findViewById(R.id.editText);
                codeEditText.setOnPinEnteredListener(new PinEntryEditText.OnPinEnteredListener() {
                    @Override
                    public void onPinEntered(CharSequence str) {
                        if (listener != null) {
                            listener.onSubmit(fragment.getEditText().getText().toString());
                        }
                    }
                });
                fragment.getEditText().addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
            }
        });
    }

    private int time = 60;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (tv_send == null) {
                return;
            }
            if (time > 0) {
                tv_send.setText(time + "s后重新获取验证码");
                tv_send.setEnabled(false);
                handler.sendEmptyMessageDelayed(0, 1000);
            } else {
                tv_send.setText("重新获取验证码");
                tv_send.setEnabled(true);
            }
            time--;
        }
    };
}
